<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="排序" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="报名截止时间" prop="signupDeadline">
        <el-date-picker clearable
          v-model="queryParams.signupDeadline"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择报名截止时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:tv:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:tv:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:tv:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:tv:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tvList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="是否开启" align="center" prop="isEnable" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报名截止时间" align="center" prop="signupDeadline" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signupDeadline, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="封面" align="center" prop="cover" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.cover" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="info" />
      
      <!-- 新增二维码列 -->
      <el-table-column label="二维码" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="viewQRCode(scope.row)"
          >查看二维码</el-button>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:tv:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:tv:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改节目类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报名截止时间" prop="signupDeadline">
          <el-date-picker clearable
            v-model="form.signupDeadline"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择报名截止时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="封面" prop="cover">
          <image-upload v-model="form.cover"/>
        </el-form-item>
        <el-form-item label="描述" prop="info">
          <el-input v-model="form.info" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTv, getTv, delTv, addTv, updateTv } from "@/api/system/tv";

export default {
  name: "Tv",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 节目类型表格数据
      tvList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sort: undefined,
        name: undefined,
        startTime: undefined,
        endTime: undefined,
        signupDeadline: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "节目名称不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ],
        signupDeadline: [
          { required: true, message: "报名截止时间不能为空", trigger: "blur" }
        ],
        cover: [
          { required: true, message: "封面不能为空", trigger: "blur" }
        ],
        info: [
          { required: true, message: "描述不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询节目类型列表 */
    getList() {
      this.loading = true;
      listTv(this.queryParams).then(response => {
        this.tvList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询节目类型详细 */
    getTv(id) {
      getTv(id).then(response => {
        this.form = response.data;
      });
    },
    /** 新增节目类型按钮点击事件 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加节目类型";
    },
    /** 修改节目类型按钮点击事件 */
    handleUpdate(row) {
      this.reset();
      const tvId = row.id || this.ids;
      this.getTv(tvId);
      this.open = true;
      this.title = "修改节目类型";
    },
    /** 提交表单 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTv(this.form).then(() => {
              this.open = false;
              this.getList();
            });
          } else {
            addTv(this.form).then(() => {
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮点击事件 */
    handleDelete(row) {
      const tvIds = row.id || this.ids;
      this.$confirm('是否确认删除节目类型编号为"' + tvIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delTv(tvIds);
      }).then(() => {
        this.getList();
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.reset();
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 分页 */
    handlePagination(val) {
      this.queryParams.pageNum = val.page;
      this.queryParams.pageSize = val.limit;
      this.getList();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        sort: undefined,
        name: undefined,
        startTime: undefined,
        endTime: undefined,
        signupDeadline: undefined,
        cover: undefined,
        info: undefined
      };
      this.resetForm("form");
    },
    resetForm(formName) {
      if (this.$refs[formName] !== undefined) {
        this.$refs[formName].resetFields();
      }
    },
    /** 导出按钮点击事件 */
    handleExport() {
      // to be implemented
    },
    /** 生成二维码 URL 的方法 */
    generateQRCodeURL(tvId, endTime) {
      //const baseUrl = "https://yxr.aiko.tv"; // 将此处替换为实际的二维码生成器 URL
      const baseUrl = "http://192.168.1.131:9090/#/pages/vist/qrcode"
      return `${baseUrl}?tvId=${tvId}&endTime=${endTime}`;
    },
    
    /** 查看二维码的方法 */
    viewQRCode(row) {
      const url = this.generateQRCodeURL(row.id, row.endTime);
      window.open(url, "_blank");
    }
  }
};
</script>
